<template>
    <view class="banner">
        <swiper class="banner-swiper" circular autoplay interval="3000" duration="500" @change="handleSwiperChange">
            <swiper-item v-for="(item, index) in list" :key="index" @click="handleSwiperItemClick(item)">
                <image :src="item.url" mode="aspectFill"></image>
            </swiper-item>
        </swiper>

        <!-- 切换按钮 -->
        <view class="swiper-nav" v-if="list.length > 1">
            <view class="nav-btn prev" @tap="handlePrev">
                <image :src="commonStore.baseImageUrl + 'test-yujia/index/index-arrow.png'" mode="widthFix"
                    width="28rpx" />
            </view>
            <view class="nav-btn next" @tap="handleNext">
                <image :src="commonStore.baseImageUrl + 'test-yujia/index/index-arrow.png'" mode="widthFix"
                    width="28rpx" />
            </view>
        </view>

        <!-- 指示点 -->
        <view class="swiper-dots" v-if="list.length > 1">
            <view v-for="(item, index) in bannerList" :key="index"
                :class="['dot', currentSwiper === index ? 'active' : '']">
            </view>
        </view>
    </view>
</template>

<script setup>
import { useCommonStore } from '@/store'
import { ref, onMounted } from 'vue'

defineProps({
    list: {
        type: Array,
        default: () => []
    }
})

const commonStore = useCommonStore()
const currentSwiper = ref(0)


// Swiper 相关方法
const handleSwiperChange = (e) => {
    currentSwiper.value = e.detail.current
}

const handlePrev = () => {
    console.log("handlePrev", currentSwiper.value, bannerList.value.length)
    currentSwiper.value = currentSwiper.value === 0
        ? bannerList.value.length - 1
        : currentSwiper.value - 1
}

const handleNext = () => {
    console.log("handleNext", currentSwiper.value, bannerList.value.length)
    currentSwiper.value = currentSwiper.value === bannerList.value.length - 1
        ? 0
        : currentSwiper.value + 1
}

const handleSwiperItemClick = (item) => {
    console.log("handleSwiperItemClick", item)
    if (item.type == 1) {
        // 显示富文本内容
        uni.navigateTo({
            url: `/pages/subpackages/mineCenter/rich-text?objectCode=${item.objectCode}`
        })
    } else if (item.type == 2) {
        // 跳转课程卡详情
        uni.navigateTo({
            url: `/pages/subpackages/mineCenter/webview?url=${item.linkUrl}`
        })
    }
}
</script>

<style lang="scss">
.banner {
    position: relative;
    border-radius: 20rpx;
    overflow: hidden;
    margin-bottom: 40rpx;

    .banner-swiper {
        width: 100%;
        height: 360rpx; // 设置合适的高度
        background-color: rgba($color: #000000, $alpha: 0.5);

        image {
            width: 100%;
            height: 100%;
        }
    }

    .swiper-nav {
        position: absolute;
        top: 50%;
        left: 0;
        right: 0;
        transform: translateY(-50%);
        display: flex;
        justify-content: space-between;
        padding: 0 20rpx;
        z-index: 2;

        .nav-btn {
            width: 60rpx;
            height: 60rpx;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;

            image {
                width: 28rpx;
                height: 28rpx;
            }

            &.next {
                transform: rotate(180deg);
            }
        }
    }

    .swiper-dots {
        position: absolute;
        bottom: 20rpx;
        display: flex;
        gap: 10rpx;
        z-index: 2;
        left: 50%;
        transform: translateX(-50%);

        .dot {
            width: 12rpx;
            height: 12rpx;
            background: #AEADA5;
            border-radius: 50%;

            &.active {
                background: #fff;
                width: 24rpx;
                border-radius: 6rpx;
            }
        }
    }
}
</style>
